package org.example.system;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.minio.errors.MinioException;
import org.example.system.common.result.Result;
import org.example.system.controller.FileController;
import org.example.system.mapper.VehicleMapper;
import org.example.system.model.dto.FileInfo;
import org.example.system.model.dto.TreeFileList;
import org.example.system.model.dto.TreeListChildren;
import org.example.system.service.OssService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

@SpringBootTest
public class TestGetFileList {

    ObjectMapper objectMapper = new ObjectMapper();
    @Autowired
    private OssService ossService;
    @Autowired
    private FileController fileController;

    @Test
    public void testGetFileList() throws MinioException, NoSuchAlgorithmException, IOException, InvalidKeyException {
        List<TreeFileList> fileList = ossService.getFileList();
        System.out.println(fileList);
    }

    @Test
    public void testGetFileListByController() throws MinioException, NoSuchAlgorithmException, IOException, InvalidKeyException {

        Result<List<TreeFileList>> fileList = fileController.getFileList();
        System.out.println(fileList);
    }

    @Test
    public void testTreeNode() throws MinioException, NoSuchAlgorithmException, IOException, InvalidKeyException {
        List<TreeFileList> fileList = ossService.getFileList();
        String jsonString = objectMapper.writeValueAsString(fileList);
        System.out.println(jsonString);
    }

    private List<TreeFileList> buildFileList(List<FileInfo> minioFileList) throws JsonProcessingException {
        List<TreeFileList> fileList = new ArrayList<>();
        Map<String, List<TreeListChildren>> folderMap = new HashMap<>();
        for (FileInfo file : minioFileList) {
            TreeFileList fileList1 = new TreeFileList();
            int isDirIndex = file.getName().indexOf("/");
            if (isDirIndex == -1) {
                // 文件
                fileList1.setId(UUID.randomUUID().toString());
                fileList1.setName(file.getName());
                fileList1.setUrl(file.getUrl());
                fileList1.setDir(false);
                fileList1.setFullPath(file.getName());
                fileList.add(fileList1);
            } else {
                // 文件夹
                String folderName = file.getName().substring(0, isDirIndex);
                if (!folderMap.containsKey(folderName)) {
                    folderMap.put(folderName, new ArrayList<>());
                    fileList1.setId(UUID.randomUUID().toString());
                    fileList1.setName(folderName);
                    fileList1.setUrl("");
                    fileList1.setDir(true);
                    fileList1.setChildren(new ArrayList<>());
                    fileList.add(fileList1);
                }

                // 添加到对应的文件夹children中
                String fileName = file.getName().substring(isDirIndex + 1);
                TreeListChildren childNode = new TreeListChildren();
                childNode.setName(fileName);
                childNode.setUrl(file.getUrl());
                childNode.setFullPath(file.getName());
                childNode.setParentPath(folderName);
                folderMap.get(folderName).add(childNode);
            }
        }

        for (TreeFileList file : fileList) {
            if (file.isDir()) {
                file.setChildren(folderMap.get(file.getName()));
            }
        }
        return fileList;
    }
}
